package com.boot.auth.endpoint;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.boot.auth.support.LoginDTO;
import com.boot.auth.support.core.PermissionService;
import com.boot.auth.support.provider.AuthenticationFactoryProvider;
import com.boot.framework.aop.Log;
import com.boot.framework.token.utils.AuthUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author yangfan
 * @date 2023/12/24
 * @Description
 */
@RestController
@RequestMapping("auth")
public class TokenEndpoint {
    // 测试登录  ---- http://localhost:8081/acc/doLogin?name=zhang&pwd=123456
    @Resource
    private PermissionService permissionService;
    @PostMapping("/login")
    @Log(title = "系统登录",content = "账号密码")
    public SaResult doLogin(@RequestBody LoginDTO login) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        AuthenticationFactoryProvider authenticationFactoryProvider = new AuthenticationFactoryProvider();
        SaTokenInfo tokenInfo = authenticationFactoryProvider.login(login);
        if (tokenInfo == null) {
            return SaResult.error("登录失败");
        }
        return SaResult.data(tokenInfo.getTokenValue());
    }

    @PostMapping("/userInfo")
    public SaResult userInfo() {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        return SaResult.data(AuthUtils.getLoginUser());
    }

    // 查询登录状态  ---- http://localhost:8081/acc/isLogin
    @RequestMapping("/router")
    public SaResult router() {
        return SaResult.data(permissionService.buildRouter());
    }



    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok();
    }
}
